﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RepertorySystem.Models;

namespace RepertorySystem.Controllers
{
    [Authorize]
    public class OrderListsController : Controller
    {
        private OrderListDbContext db = new OrderListDbContext();

        // GET: OrderLists
        public ActionResult Index()
        {
            int arryLength = db.OrderLists.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<OrderList> content = db.OrderLists.OrderByDescending(u => u.Time).Skip(0).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.totalPages = totalPages;
            ViewBag.currentPage = 1;

            return View();
        }

        public ActionResult GetNext(int currentPage) {
            int arryLength = db.OrderLists.Count();
            int totalPages;
            if (arryLength % 5 != 0) {
                totalPages = (arryLength / 5) + 1;
            } else {
                totalPages = (arryLength / 5);
            }

            List<OrderList> content = db.OrderLists.OrderByDescending(u => u.Time).Skip((currentPage - 1) * 5).Take(5).ToList();
            ViewBag.content = content;
            ViewBag.currentPage = currentPage;
            ViewBag.totalPages = totalPages;
            return View("Index");

        }

        public ActionResult FindOne(String book_Name) {
            //查询数据
            this.TempData["book_Name"] = book_Name;
            //跳转
            return RedirectToAction("FindOneOrderLists", "BookModels", "");

        }

        public ActionResult ReturnOne() {
            BookModels book = new BookModels();
            book = this.TempData["resultBook"] as BookModels;
            int id = book.ID;

            List<OrderList> accountList = db.OrderLists.Where(a => a.Book_Id == book.ID).ToList();


            ViewBag.content = accountList;
            ViewBag.currentPage = 1;
            ViewBag.totalPages = 1;


            return View("Index");
        }

        public ActionResult ReturnNoFind() {

            ViewBag.content = null;
            ViewBag.currentPage = 0;
            ViewBag.totalPages = 0;
            return View("Index");
        }

        // GET: OrderLists/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderList orderList = db.OrderLists.Find(id);
            if (orderList == null)
            {
                return HttpNotFound();
            }
            return View(orderList);
        }

        // GET: OrderLists/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: OrderLists/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Procurer,Book_Id,Book_Number,Time,Price,State")] OrderList orderList)
        {
            if (ModelState.IsValid)
            {
                db.OrderLists.Add(orderList);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(orderList);
        }

        // GET: OrderLists/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderList orderList = db.OrderLists.Find(id);
            if (orderList == null)
            {
                return HttpNotFound();
            }
            return View(orderList);
        }

        // POST: OrderLists/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Procurer,Book_Id,Book_Number,Time,Price,State")] OrderList orderList)
        {
            //得到订单的id
            var id = orderList.ID;
            //查询数据
            var sto = db.OrderLists.Where(ord => ord.ID == id).FirstOrDefault();
          
            if (sto.State == 0) {
                sto.State = 1;
                //修改订单状态
                db.SaveChanges();
                
                //添加新账目
                AccountList accountList = new AccountList();
                accountList.Time = orderList.Time;
                accountList.Category = 0;
                accountList.Book_Id = orderList.Book_Id;
                accountList.Book_Number = orderList.Book_Number;
                accountList.Price = orderList.Price;
                //将新账目通过TempData发送到AccountList控制器
                this.TempData["accountList"] = accountList;
                //跳转
                return RedirectToAction("Create","AccountLists","");

            }

            return RedirectToAction("Index");
        }

        // GET: OrderLists/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderList orderList = db.OrderLists.Find(id);
            if (orderList == null)
            {
                return HttpNotFound();
            }
            return View(orderList);
        }

        // POST: OrderLists/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            OrderList orderList = db.OrderLists.Find(id);
            db.OrderLists.Remove(orderList);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
